<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
* {
	font-size: 14px;
}

div {
	margin: 0 auto;
	width: 800px;
	margin-bottom: 5px;
	padding: 2px;
}

.h {
	text-align: right;
}

h3 {
	text-align: center;
}

table {
	border: 1px solid blue;
	border-collapse: collapse;
	margin: 5px;
	width: 800px;
}

td {
	border: 1px solid blue;
}

input#title, textarea {
	border: 1px solid red;
}
.nav{
	margin:0 auto;
	text-align:center;
}
.active{
	text-decoration:none;
}
</style>

</head>
<body>
	<div class="h">
		<a href="${ctx}/login.jsp">登录</a> &nbsp;&nbsp;
		<a href="${ctx}/reg.jsp">注册</a>&nbsp;&nbsp;
		<a href="${ctx}/user?type=getAll&page=1">用户管理</a>&nbsp;&nbsp;
		<a href="${ctx}/role?type=getAll&page=1">角色管理</a>&nbsp;&nbsp;
		欢迎您，${sessionScope.loginInfo.username} <a
			href="${ctx}/user?type=logout">注销</a>
	</div>

	<c:set var="page" value="${page}"></c:set>
	<c:set var="totalRecords" value="${totalRecords}"></c:set>
	<c:set var="pageSize" value="${pageSize}"></c:set>
	<c:set var="totalPages" value="${totalPages}"></c:set>
	<c:set var="beginIndex" value="${beginIndex}"></c:set>
	<c:set var="endIndex" value="${endIndex }"></c:set>
	<c:set var="messageList" value="${messageList }"></c:set>
	
	<div>
		<h3>留言列表</h3>
		<c:forEach items="${messageList}" var="obj">
			<table>
				<tr>
					<td width="60px">标题：</td>
					<td>${obj.title}</td>
				</tr>
				<tr>
					<td>内容：</td>
					<td>${obj.content}</td>
				</tr>
				<tr>
					<td>时间：</td>
					<td>${obj.createDateTime}</td>
				</tr>
				<tr>
					<td>作者：</td>
					<td>${obj.username}</td>
				</tr>
				<c:if
					test="${sessionScope.loginInfo.roleId eq 1 or sessionScope.loginInfo.id eq obj.userId}">
					<tr>
						<td>操作：</td>
						<td><a href="${ctx}/message?type=del&id=${obj.id}"
							onclick="return confirm('您确定要删除该记录${obj.id}吗？');">删除</a> <a
							href="${ctx}/message?type=get&id=${obj.id}">编辑</a></td>
					</tr>
				</c:if>

			</table>
		</c:forEach>
	</div>
	<div class="nav">
		共有记录${totalRecords}条，共${totalPages}页，
		<a href="${ctx}/index?page=1">首页</a>
		<a href="${ctx}/index?page=${page-1<1?1:page-1}">上页</a>
		<c:forEach begin="1" end="${totalPages}" var="obj">
			<c:set var="active" value="${page==obj?'active':''}"></c:set>
			<c:set var="tureOrFalse" value="${page==obj?'return false;':'return true;'}"></c:set>				
			<a class="${active}" href="${ctx}/index?page=${obj}" onclick="${tureOrFalse}">${obj}</a>
		</c:forEach>
		<a href="${ctx}/index?page=${page+1>totalPages?totalPages:page+1}">下页</a>
		<a href="${ctx}/index?page=${totalPages}">末页</a>
	</div>

	<form action="${ctx}/message?type=add" method="post"
		onsubmit="return checkLogin();">
		<div>
			<table>
				<tr>
					<td width="60px">标题：</td>
					<td><input type="text" name="title" id="title" size="50"></td>
				</tr>
				<tr>
					<td>内容：</td>
					<td><textarea rows="10" cols="100" name="content" id="content"></textarea>
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center"><input type="submit" value="发表">
					</td>
				</tr>
			</table>
		</div>
	</form>
</body>
<script type="text/javascript">
	function checkLogin() {
		var name = "${sessionScope.loginInfo.username}";
		alert(name);
		if (name == null || name == "") {
			alert("请先登录！");
			window.location = "${ctx}/login.jsp";
			return false;
		} else {
			return true;
		}
	}
</script>
</html>
